package cn.lizhangqu.kb.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;

import cn.lizhangqu.kb.R;
import cn.lizhangqu.kb.util.CommonUtil;
import cn.lizhangqu.kb.util.HttpUtil;
import cn.lizhangqu.kb.util.SharedPreferenceUtil;

public class SplashActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);

		getViewStatus();
		((ImageView) findViewById(R.id.bg)).postDelayed(new Runnable() {

			@Override
			public void run() {
				SharedPreferenceUtil util = new SharedPreferenceUtil(getApplicationContext(), "accountInfo");
				String isLogin = util.getKeyData("isLogin");
				//是否已登录
//				if (isLogin.equals("TRUE")) {
//					Intent intent = new Intent(SplashActivity.this, MainActivity.class);
//					startActivity(intent);
//					finish();
//				} else {
					CommonUtil.jump2Intent(SplashActivity.this, LoginActivity.class);
					finish();
//				}

			}
		}, 3000);

	}


	public void getViewStatus() {

		AsyncHttpClient client = new AsyncHttpClient();

		client.get("http://210.34.213.105/default2.aspx", new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
//				Log.e("yyy", responseString);

				int index = responseString.indexOf("<input type=\"hidden\" name=\"__VIEWSTATE\" ");

				Log.v("yyy", "================>"+index);

				// , "dDwyODE2NTM0OTg7Oz4DPMVq8ywhFPTcCKepx90x+bYM5g==".length()

				String code = responseString.substring(index + "<input type=\"hidden\" name=\"__VIEWSTATE\" value=\"".length());

				index = code.indexOf("\" />");

				code = code.substring(0, index);

				Log.v("yyy", code);
				HttpUtil.__VIEWSTATE = code;
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				Log.e("xxx", responseString);
			}
		});
	}

//	public static String getCode(String content) {
//		int index = content.indexOf("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
//
//		Log.v("yyy", "================>"+index);
//
//		// , "dDwyODE2NTM0OTg7Oz4DPMVq8ywhFPTcCKepx90x+bYM5g==".length()
//
//		String code = content.substring(index + "<input type=\"hidden\" name=\"__VIEWSTATE\" value=\"".length());
//
//		index = code.indexOf("\" />");
//
//		code = code.substring(0, index);
//
//		Log.v("yyy", code);
//	}
}
